home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0011_LOCKFILE.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  1KB  |  37 lines

  1. {
  2. > Does anyone have any multi-tasking/File sharing Units (preferably
  3. > With well documented code).  Specifically, I need to Write a Program
  4. > that _may_ be active on one node, and I'd like to open the Files in
  5. > read-only Form, amung other things, so that I can load that in
  6. > multi-node (shared) environment.
  7.  
  8. }
  9.  
  10. Function LockFile(f : File) : Boolean;  { returns True if lock achieved. }
  11.                                         { if not, File locked by other   }
  12.                                         { application running.           }
  13.  
  14. Var
  15.   r : Registers;   {Defined in Dos Unit}
  16.   l : LongInt;
  17.  
  18. begin
  19.   r.ah := $5C;
  20.   r.al := 0;
  21.   Move(f,r.bx,2);   {Places File handle into BX register.}
  22.   r.cx := 0;  {Most significant, region offset (0 - beginning of File)}
  23.   r.dx := 0;  {Least significant, region offset (0 - beginning of File)}
  24.   l := FileSize(f);         { Get File size }
  25.   r.di := l and $ffff;      { Devide File size to most/least parts }
  26.   r.si := l div $10000;     { For locking the entire File.         }
  27.   MsDos(r);
  28.   LockFile := ((r.flags and 1)=0);
  29.   { if carry flag is set File locking failed, reason in AX }
  30. end;
  31.  
  32. {
  33. BTW: to unlock it use the same routine, but change the  r.al to 1.
  34.  
  35. if this routine fails, it means that the File is locked in the other
  36. task, and cannot be used.
  37. }